I Actually Did It: Neural Compression That Works And I Have No Clue Why
I did it. I actually did the thing. I trained a neural network to compress files into PNG images and it works. Like actually works. Zero percent quality drop on files up to 100MB. I have no fucking clue how this works but it does.
When you joke about an idea in a blog post (that never got posted because I changed it last second) and then actually build it because you cannot sleep, you realize you have too much time and not enough sense. This is my life now.
The Results
Here is what happens. You give it a file. Any file. Text. Images. Whatever. It trains on random UTF-8 characters in varying lengths. Then it compresses your file into a PNG image. The PNG can be anywhere from zero bits to 12MB. Even if your original file is 100MB.
The model somehow learned to know when a file is larger than 12MB and compresses it anyway. With zero percent loss. I do not understand this. I do not understand how a neural network learned to do lossless compression better than I expected.
The Math (Which I Barely Understand)
Here is the cool part. One pixel at 1 bpb represents 24 bytes of original data. Let me break that down:
At 1 bpb (bit per byte):
- 1 bit per original byte = 1/8 byte compressed
- 3 bytes per RGB pixel
- 1 pixel represents 24 bytes of original data
For a 1MB file at 1 bpb:
- Compressed payload: 125KB
- Pixels needed: ~42K pixels (e.g., 256×256 = 65K pixels)
The PNG container is very efficient at storing the compressed data as pixels!
# I still do not fully understand this.
The model has been trained on part of FineWeb-Edu and has gotten to a bpb of 1.201. That is bits per byte. Lower is better. 1.201 is actually pretty good for a neural compression method.
The New Architecture
I changed things. The model is now a 4-layer GRU with 512 hidden units per layer. That is bigger. That is better. That is more confusion for me to explain.
But here is the really weird part. The model now embeds itself into the PNG file. Yes. The model saves itself inside the compressed image. Because the model adapts itself to whatever you are compressing most often.
Step 1: Take the pretrained model
Step 2: Compress a file
Step 3: Model trains itself for 50 steps on YOUR data
Step 4: Improves compression ratio for YOUR specific needs
Step 5: Compresses the file
Step 6: Saves itself to the image
Step 7: Loads the old model so it can re-load the data
Step 8: Marvel at the fact it works
# I still do not fully understand this.
Small files will become larger. The model is 25MB. If you compress a 1MB file, it becomes 25MB plus compressed data. That is the trade-off. That is the cost of having a model that adapts to you.
I made it viewable because I could. Because it is fun. Because watching someone try to open your "homework" PNG and see colorful static is the best part. The ZPNG magic header prevents random images from decompressing. Your cat photos are safe.
How To Use It
It is plug and play. No special arguments. No complex configuration. Just install and go.
pip install torch constriction pillow numpy
# Train the model (or pull from HuggingFace)
python train.py
# Compress a file
python inference.py compress myfile.txt -o myfile.zpng.png
# Decompress
python inference.py decompress myfile.zpng.png -o restored.txt
# Done. Your file is back. Hopefully.
Or just run python compressor.py for an interactive menu. It is vaguely intuitive if you squint.
The Architecture
Four-layer GRU with 512 hidden units per layer. Range coding via Constriction. The compressed payload lives in RGB pixel bytes. The PNG has a ZPNG magic header in the first 4 bytes so random images do not decompress. The model embeds itself in the PNG so it can adapt to your data.
Text files compress okay. Binary files less okay. Random data might actually grow. That is the fun part. Compression ratio varies. Your mileage may vary. Your data may vary. Everything varies.
Important Notes
If your input is not larger than the max it can store, it WILL compress. Just a very small amount. Re-compress if it is not small enough. Enable temp or something. LOL.
Small files will become larger because the model is 25MB. The model embeds itself. The model adapts to your data. The model trains for 50 steps on your specific file. Then it saves itself. Then it compresses. Then it loads the old model back. It is complicated. It works. I do not know why.
Models: https://huggingface.co/collections/CompactAI-O/zip-model-collection
Source code is here. Models are here. Train your own. Adjust the sizes. Have fun. Break things. I dare you.
Why This Exists
Because someone on the internet asked if you can compress files with a neural network. Because I said "bet, easy". Because it turns out it is actually hard. Because I could not let it go. Because now we are here.
Also I wanted to use range coding once. Constriction is neat. The journey was fun. The result is amusing. Now you get to find out too.
Do not forget your model. Without model.pt your zpng.png files are colorful but useless. Lossiness is possible if training was short or data weird. Check with checksums. GPU recommended. CPU training is slow enough to notice.
Final Thoughts
I built a neural file compressor. It works. It compresses 100MB files into 12MB PNGs with zero percent loss. The model is now 4-layer GRU with 512 hidden. The model embeds itself in the PNG. The model adapts to your data by training for 50 steps on your specific file. I have no fucking clue how it works.
The compressed files are viewable PNGs with colorful static. Black space for unused space. Random colors that correspond to data. The model saves itself inside. It is art. It is science. It is both.
Anyone can train this model. Anyone can adjust the sizes. No special arguments needed. Just plug and play. Just train. Just compress. Just decompress. Just voila.
Go try it. Go break it. Go train your own. Go figure out how it works because I still do not. Go have fun. That is the point. That is the whole point.